/*
 * Copyright (C) 2020-present, kinbug All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitee.kinbug.sniper.common.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import jodd.http.HttpRequest;

/**
 * -描述
 * @ClassName: DingTalkUtil
 * @author kinbug
 * @date 2020-11-17
 */
public class DingTalkUtil {

	/**
	 * -发送钉钉消息通知
	 * @Title sendMsg
	 * @param content 消息内容
	 * @param isAtAll 是否通知所有人
	 * @param mobileList 具体通知那些人
	 * @return void 返回类型
	 * @throws
	 */
	public static void sendMsg(String url,String content, boolean isAtAll, List<String> mobileList) {
		// 组装请求内容
		String reqStr = buildReqStr(content, isAtAll, mobileList);
		// 推送消息（http请求）
		HttpRequest request = HttpRequest.post(url);
		request.contentType("application/json").charset("UTF-8").bodyText(reqStr).send();
	}

	/**
	 * 组装请求报文
	 * @param content
	 * @return
	 */
	private static String buildReqStr(String content, boolean isAtAll, List<String> mobileList) {
		// 消息内容
		Map<String, String> contentMap = new HashMap<String, String>(10);
		contentMap.put("content", content);
		// 通知人
		Map<String, Object> atMap = new HashMap<String, Object>(10);
		// 1.是否通知所有人
		atMap.put("isAtAll", isAtAll);
		// 2.通知具体人的手机号码列表
		atMap.put("atMobiles", mobileList);
		Map<String, Object> reqMap = new HashMap<String, Object>(10);
		reqMap.put("msgtype", "text");
		reqMap.put("text", contentMap);
		reqMap.put("at", atMap);
		return JSON.toJSONString(reqMap);
	}
	
}
